﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для wCash.xaml
    /// </summary>
    public partial class wCash : Window
    {
        private routelistEntities routelistEntities;
        private Cash cash;


        public int? CashID
        {
            get { return (int?)GetValue(CashIDProperty); }
            set { SetValue(CashIDProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CashID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CashIDProperty =
            DependencyProperty.Register("CashID", typeof(int?), typeof(wCash), new UIPropertyMetadata(null, new PropertyChangedCallback(propIDChanged)));

        public static void propIDChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            wCash item = d as wCash;
            if (null == item) return;
            int? val = (int?)e.NewValue;
            if (null == val)
            {
                item.cash = null;
                return;
            }
            item.cash = (from o in item.routelistEntities.Cash where o.id == val select o).FirstOrDefault();
        }

        private void LoadData()
        {
            IEnumerable<RouteList> rl;
            if (null == cash) return;
            DateTime beg_date = cash.datedoc.Date;
            DateTime end_date = beg_date.AddDays(1);
            rl = from o in routelistEntities.RouteList where o.dt_datedoc >= beg_date && o.dt_datedoc < end_date select o;
            var clear = (from o in routelistEntities.Cash where o.datedoc == cash.datedoc select o).AsEnumerable();
            foreach (Cash it in clear)
            {
                routelistEntities.DeleteObject(it);
            }
            var dt = from o in routelistEntities.RouteList join a in routelistEntities.RouteListInfo on o.id equals a.id_rl orderby o.nomdog, a.Routes.number select a;
            foreach (RouteListInfo it in dt)
            {
                if (it.id_route == null) continue;
                Cash newit = new Cash();
                newit.datedoc = cash.datedoc;
                newit.id_Route = it.id_route.Value;
                newit.id_RouteList = it.id_rl;
                newit.summa = it.price_fact != null ? it.price_fact.Value : 0;
                newit.qpassengers = (int)(it.Routes.price == null ? 0 : newit.summa / it.Routes.price.Value);
                routelistEntities.Cash.AddObject(newit);
            }
        }

        private void SaveData()
        {
            try
            {
                if (null == cash) return;
                var rslt = from o in routelistEntities.Cash where o.datedoc == cash.datedoc select o;
                foreach (Cash it in rslt)
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public wCash()  
        {
            InitializeComponent();
            routelistEntities = new routelistEntities();
            cash=null;
        }
    }
}
